using System.Collections.Generic;
using UnityEngine;

public class TouchHandler : MonoBehaviour
{
	private static TouchHandler instance;

	private static GameObject parentObject;

	public float tapTimeLimit = 0.4f;

	public float tapMagnitudeLimit = 10f;

	public bool isScreenTouched;

	public Vector2 lastTouchStartPoint = Vector2.zero;

	public float lastTouchStartTime;

	public TouchDetails lastTouch;

	public List<TouchDetails> lastTouches = new List<TouchDetails>();

	private bool shortTapPossible;

	public static float idleSince;

	public float idleTime;

	private int previousNumberOfTouches;

	private int handeledTouches;

	private Vector3 previousTouchPosition = Vector3.zero;

	private float previousTouchDistance;

	public static TouchHandler GetInstance()
	{
		if (!instance)
		{
			parentObject = GameObject.Find("MainManager");
			if (parentObject != null)
			{
				instance = parentObject.GetComponent(typeof(TouchHandler)) as TouchHandler;
			}
			else
			{
				Debug.Log("WTF : no instance found for TouchHandler");
			}
		}
		return instance;
	}

	private void Start()
	{
		idleSince = Time.realtimeSinceStartup;
		Input.multiTouchEnabled = false;
		tapMagnitudeLimit *= Utils.GetDPIratio();
	}

	private void Update()
	{
		int num = 20;
		Utils.TouchState touchState = Utils.getTouchState();
		if (touchState != 0)
		{
			Vector2 touchPositionVector = Utils.getTouchPositionVector2();
			float time = Time.time;
			TouchDetails touchDetails = new TouchDetails(touchPositionVector, time, lastTouchStartPoint, lastTouchStartTime, touchState);
			switch (touchState)
			{
			case Utils.TouchState.touchBegin:
				isScreenTouched = true;
				lastTouchStartPoint = touchPositionVector;
				lastTouchStartTime = time;
				touchDetails.startPoint = lastTouchStartPoint;
				touchDetails.startTime = lastTouchStartTime;
				if (lastTouches.Count > num)
				{
					lastTouches.RemoveAt(0);
				}
				lastTouches.Add(touchDetails);
				if (Input.touchCount == 1 || Input.GetMouseButton(0))
				{
					shortTapPossible = true;
				}
				break;
			case Utils.TouchState.touchEnd:
				isScreenTouched = false;
				if (lastTouches.Count > num)
				{
					lastTouches.RemoveAt(0);
				}
				touchDetails.point = lastTouch.point;
				lastTouches.Add(touchDetails);
				break;
			default:
				isScreenTouched = true;
				if (lastTouches.Count > num)
				{
					lastTouches.RemoveAt(0);
				}
				lastTouches.Add(touchDetails);
				break;
			}
			lastTouch = touchDetails;
		}
		if (Input.touchCount > 0)
		{
			idleSince = Time.realtimeSinceStartup;
			if (Input.touchCount == 1)
			{
				if (handeledTouches == 2)
				{
					EventManager.FireTwoTouchEnd(previousTouchPosition, previousTouchDistance);
				}
				Touch touch = Input.touches[0];
				if (touch.phase == TouchPhase.Began)
				{
					EventManager.FireOneTouchBegin(touch.position);
				}
				if (touch.phase == TouchPhase.Moved)
				{
					EventManager.FireOneTouchMove(touch.position);
				}
				if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
				{
					EventManager.FireOneTouchEnd(touch.position);
				}
				handeledTouches = 1;
				previousTouchPosition = touch.position;
				previousTouchDistance = 0f;
			}
			else if (Input.touchCount == 2)
			{
				if (handeledTouches == 1)
				{
					EventManager.FireOneTouchEnd(previousTouchPosition);
				}
				Touch touch2 = Input.touches[0];
				Touch touch3 = Input.touches[1];
				Vector3 touchPosition = (touch2.position + touch3.position) / 2f;
				float touchDistance = Vector2.Distance(touch2.position, touch3.position);
				if (previousNumberOfTouches != 2)
				{
					EventManager.FireTwoTouchBegin(touchPosition, touchDistance);
				}
				else
				{
					EventManager.FireTwoTouchMove(touchPosition, touchDistance);
				}
				handeledTouches = 2;
				previousTouchPosition = touchPosition;
				previousTouchDistance = touchDistance;
			}
			else
			{
				if (handeledTouches == 2)
				{
					EventManager.FireTwoTouchEnd(previousTouchPosition, previousTouchDistance);
				}
				if (handeledTouches == 1)
				{
					EventManager.FireOneTouchEnd(previousTouchPosition);
				}
				handeledTouches = 0;
				previousTouchPosition = Vector3.zero;
				previousTouchDistance = 0f;
			}
		}
		else
		{
			if (handeledTouches == 2)
			{
				EventManager.FireTwoTouchEnd(previousTouchPosition, previousTouchDistance);
				handeledTouches = 0;
			}
			if (Input.GetMouseButtonDown(0))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireOneTouchBegin(Input.mousePosition);
			}
			else if (Input.GetMouseButton(0))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireOneTouchMove(Input.mousePosition);
			}
			else if (Input.GetMouseButtonUp(0))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireOneTouchEnd(Input.mousePosition);
			}
			if (Input.GetMouseButtonDown(1))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireTwoTouchBegin(Input.mousePosition, 0f);
			}
			else if (Input.GetMouseButton(1))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireTwoTouchMove(Input.mousePosition, 0f);
			}
			else if (Input.GetMouseButtonUp(1))
			{
				idleSince = Time.realtimeSinceStartup;
				EventManager.FireTwoTouchEnd(Input.mousePosition, 0f);
			}
		}
		previousNumberOfTouches = Input.touchCount;
		if (shortTapPossible && Input.touchCount != 1 && !Input.GetMouseButton(0))
		{
			shortTapPossible = false;
		}
		if (shortTapPossible)
		{
			if (lastTouch.time - lastTouch.startTime >= tapTimeLimit)
			{
				shortTapPossible = false;
			}
			else if ((lastTouch.point - lastTouch.startPoint).magnitude > tapMagnitudeLimit)
			{
				shortTapPossible = false;
			}
		}
		idleTime = Time.realtimeSinceStartup - idleSince;
	}

	public static float GetIdleTime()
	{
		return Time.realtimeSinceStartup - idleSince;
	}
}
